
let {User,questionAll,CountNum,loneCheck} = require('../models');
let auth=require('../utils/auth');

let index_fn = async (ctx, next) => {
    let username=ctx.session.userName;


 
 
let obj={
    username:username,
};

    ctx.render('orindex.html',obj);
}
let login_fn = async (ctx, next) => {
    ctx.render('login.html');
}
let register_fn = async (ctx, next) => {
    ctx.render('register.html')
}

let loginDone_fn = async (ctx, next) => {
    let username = ctx.request.body.username || '';
    let password = ctx.request.body.password || '';

    console.log(username);
    console.log(password);
    let res;
    let u1=await User.findOne({where:{
        username:username,
        password:password
    }}).then((row)=>{
        let user=JSON.stringify(row);
        let u2=JSON.parse(user);
        console.log(user);
        console.log(u2);
        
        if(user!=='null'){
            ctx.session.userId=row.id;
            ctx.session.userName=row.username;
            res={code:200,msg:'登录成功'};
        }else{
            res={code:1000,msg:'用户名或密码错误，请重试'};
        }
    });
    ctx.body=res;
}
let registerDone_fn = async (ctx, next) => {
    let username = ctx.request.body.username || '';
    let password = ctx.request.body.password || '';
    let confirmpassword = ctx.request.body.confirmpassword || '';

    console.log(username);
    console.log(password);
    console.log(confirmpassword);

    if (username.length > 0 && password.length > 0 && confirmpassword.length > 0 && password === confirmpassword) {
        let userDemo=await User.findOne({ where: { username: username } });
        console.log(JSON.stringify(userDemo));
        let res='';

        // 如果有找到同名的用户，则返回提示消息，并且不创建用户；否则创建用户，并返回提示消息
        if(userDemo){
            // msg='当前用户名已经注册，请确认后重试';
            res={code:1000,msg:'当前用户名已经注册，请确认后重试'};
        }else{
            let u1=User.create({
                username: username,
                password: password
            });
            res={code:200,msg:'注册成功'};
        }

        ctx.body=JSON.stringify(res);
        // ctx.render('login.html');
    } else {
        console.log('用户名或密码不能为空；并且两次密码应该相同');
    }
}
let logout_fn=async (ctx,next)=>{
    ctx.session=null;

    ctx.body={code:200,msg:'注销成功'};
}

let forgot_fn = async (ctx, next) => {
    ctx.render('forgot.html')
}

let introduce_fn = async (ctx,next) => {
    ctx.render('introduce.html')

}

let questionnaire_fn = async (ctx,next) => {
    ctx.render('questionnaire.html')

}

let modelw_fn = async(ctx,next) => { 
    ctx.render('modelw.html')
}
let qiye_fn =async(ctx,next) => { 
    ctx.render('qiye.html')
}
let zengzhi_fn =async(ctx,next) => { 
    ctx.render('zengzhi.html')
}
let cankao_fn =async(ctx,next) => { 
    ctx.render('cankao.html')
}
let help_fn =async(ctx,next) => { 
    ctx.render('help.html')
}
let liaojie_fn =async(ctx,next) => { 
    ctx.render('liaojie.html')
}



let charts_fn =async(ctx,next) => { 
    let username=ctx.session.userName;

    
    let obj={
        username:username,

    };

    ctx.render('charts.html',obj);

}
let createTable_fn =async(ctx,next) => { 
    let username=ctx.session.userName;

    
    let obj={
        username:username,

    };

    ctx.render('createTable.html',obj)
}
let tables_fn =async(ctx,next) => { 
    let username=ctx.session.userName;

   // 查找问卷主体框架所有内容
   let questionAllsh=JSON.parse(JSON.stringify(await questionAll.findAll()));
   // 查找所有单选题
   let lonecheckh=JSON.parse(JSON.stringify(await loneCheck.findAll()));
   // 查找所有的评分题
   let countNumh=JSON.parse(JSON.stringify(await CountNum.findAll()));


let obj={
    username:username,
    questionAllq:questionAllsh,
    CountNumq:countNumh,
    loneCheckq:lonecheckh

};
    ctx.render('tables.html',obj)
}
let widgets_fn =async(ctx,next) => { 
    let username=ctx.session.userName;

    
    let obj={
        username:username,

    };
    ctx.render('widgets.html',obj)
}


let create_fn =async(ctx,next) => { 
    let username=ctx.session.userName;

    // 查找问卷主体框架所有内容
    let questionAllsh=JSON.parse(JSON.stringify(await questionAll.findAll()));
    // 查找所有单选题
    let lonecheckh=JSON.parse(JSON.stringify(await loneCheck.findAll()));
    // 查找所有的评分题
    let countNumh=JSON.parse(JSON.stringify(await CountNum.findAll()));

 
 let obj={
     username:username,
     questionAllq:questionAllsh,
     CountNumq:countNumh,
     loneCheckq:lonecheckh

 };
 
    ctx.render('create.html',obj)
}


let loneCheckDone_fn = async(ctx,next) =>{
    let problem1 = ctx.request.body.one || '';
    let problem2 = ctx.request.body.two || '';
    let problem3 = ctx.request.body.three || '';
    let problem4 = ctx.request.body.four || '';
    let que = ctx.request.body.quetions || '';
    


    console.log(problem1);
    console.log(problem2);
    console.log(problem3);
    console.log(problem4);
    console.log(que);

    let modelData = await loneCheck.create({
        question:que,
        Check1:problem1,
        Check2:problem2,
        Check3:problem3,
        Check4:problem4,
    });

    let data = JSON.stringify(modelData);
    ctx.body = {code:200,data,msg:'发布问题成功'}

}

let createDone_fn = async(ctx,next) =>{
    let title = ctx.request.body.title || '';
    let type = ctx.request.body.type || '';
    let state = ctx.request.body.state || '';
    let suggest = ctx.request.body.suggest || '';


    let problem1 = ctx.request.body.one || '';
    let problem2 = ctx.request.body.two || '';
    let problem3 = ctx.request.body.three || '';
    let problem4 = ctx.request.body.four || '';
    let que = ctx.request.body.quetions || '';
  
    console.log(title);
    console.log(type);
    console.log(state);
    console.log(suggest);

    let modelData = await loneCheck.create({
        question:que,
        Check1:problem1,
        Check2:problem2,
        Check3:problem3,
        Check4:problem4,
    });

    let modelData1 = await questionAll.create({
        Title:title,
        Type:type,
        State:state,
        Suggest:suggest

    });

    let data = JSON.stringify(modelData,modelData1);
    ctx.body = {code:200,data,msg:'发布问卷成功'}

}

module.exports = {
    '/': ['get',auth, index_fn],
    '/login': ['get', login_fn],
    '/logout': ['post', logout_fn],
    '/register': ['get', register_fn],
    '/loginDone': ['post', loginDone_fn],
    '/registerDone': ['post', registerDone_fn],
    '/forgot':['get',forgot_fn],
    '/introduce':['get',introduce_fn],
    '/questionnaire':['get',questionnaire_fn],
    '/qiye':['get',qiye_fn],
    '/zengzhi':['get',zengzhi_fn],
    '/cankao':['get',cankao_fn],
    '/modelw':['get',modelw_fn],
    '/help':['get',help_fn],
    '/liaojie':['get',liaojie_fn],
    '/charts':['get',auth,charts_fn],
    '/createTable':['get',auth,createTable_fn],
    '/tables':['get',auth,tables_fn],
    '/widgets':['get',auth,widgets_fn],
    '/create':['get',auth,create_fn],
    '/loneCheckDone':['post',auth,loneCheckDone_fn],
    '/createDone':['post',auth,createDone_fn],


}